package com.lg.system.main.controller;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lg.common.base.BaseController;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Update;
import com.lg.system.main.entity.Online;
import com.lg.system.main.service.OnlineService;
import com.lg.system.user.service.UserRoleService;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dl
 * @since 2018-03-28
 */
@RestController
@RequestMapping("/online")
public class OnlineController extends BaseController<Online, OnlineService> {
	private String prefix="system/main/online";
	
	@Autowired
	OnlineService onlineService;
	
	@Autowired
	UserRoleService userRoleService;
	
	@RequiresPermissions("online:online")
	@GetMapping("index")
	public ModelAndView index(){
		onlineService.initTable();
		ModelAndView mView=new ModelAndView(prefix+"/index");
		return mView;
	}
	
	@RequiresPermissions("online:online")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody Online online){
		Query<Online> query = new Query<Online>(online);
		List<Online> users=onlineService.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(users,query.getPage().getTotal());
		return pa;
	}
	
	@RequiresPermissions("online:edit")
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Long id){
		ModelAndView mView=new ModelAndView(prefix+"/edit");
		Online online=onlineService.selectById(id);
		mView.addObject("obj",online);
		return mView;
	}
	
	@RequiresPermissions("online:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) Online online,BindingResult bindingResult){
		if(onlineService.updateById(online)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	/** 
	* @Description: 根据模板生成代码
	* @param @param id  
	* @return void 
	* @throws 
	*/ 
	@RequiresPermissions("online:auto")
	@GetMapping("/down")
	public void down(Long id){
		Online online=onlineService.selectById(id);
		if(online!=null){//
			onlineService.down(online);
		}
	}
	
	@RequiresPermissions("online:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) Online online,BindingResult bindingResult){
		if(onlineService.deleteById(online.getId())){
			return R.ok();
		}else{
			return R.error();
		}
	}
}
